## Create the gcd and lcm function using Rcpplibrary(Rcpp)## Include these functionssourceCpp("gcd_lcm.cpp")## Create the 'rational' constructorsetClass("rational",contains ="numeric",slots =c(numerator ="numeric",denominator ="numeric" ),## check to ensure 0 isn't in denominatorvalidity =function(object) {if (object@denominator ==0) {stop("Denominator cannot be zero.") }## check to ensure complex numbers aren't usedif (is.complex(object@numerator) ||is.complex(object@denominator)) {stop("Numerator and denominator cannot be complex numbers.") }TRUE })## 'show' method for clean outputssetMethod("show", "rational",function(object) {cat(object@numerator, "/", object@denominator, "\n") })## Streamlined 'simplify' method for rational objectssetGeneric("simplify", function(object) standardGeneric("simplify"))
Error in validityMethod(object): Denominator cannot be zero.
Code
quotient(r1)
4
Code
quotient(r2)
0.0304348
Code
quotient(r2, digits =3)
0.03
Code
quotient(r2, digits =3.14)
0.03
Code
quotient(r2, digits ="avocado")
Error in round(result, digits): non-numeric argument to mathematical function
Code
q2 <-quotient(r2, digits =3)
0.03
Code
q2
NULL
Code
quotient(r3)
0
Code
simplify(r1)
4 / 1
Code
simplify(r2)
7 / 230
Code
simplify(r3)
0 / 1
Part C
Code
## Check for Zero in denominatorr4 <-new("rational", numerator =24, denominator =0)
Error in validityMethod(object): Denominator cannot be zero.
Code
## Check for Complex numberr5 <-new("rational", numerator =2i, denominator =1)
Error in validObject(.Object): invalid class "rational" object: invalid object for slot "numerator" in class "rational": got class "complex", should be or extend class "numeric"
Problem 2 - plotly
Part A: Any Change in the Distribution of Genre Of Sales?
Note: Professor Errickson’s plot was used for this analysis as allowed by the problem instructions.
As such the ‘gg’ object will be passed into the ‘ggplotly’ function.
Code
library(plotly)
Warning: package 'plotly' was built under R version 4.4.2
Loading required package: ggplot2
Attaching package: 'plotly'
The following object is masked from 'package:ggplot2':
last_plot
The following object is masked from 'package:stats':
filter
The following object is masked from 'package:graphics':
layout
Code
library(ggplot2)## Professor Errickson's Plot was usedart <-read.csv("data/art.csv")## Cleanup Dataart$Genre___Others[art$Genre___Painting ==1] <-0unique(art[, grep("^Genre", names(art))])
## Obtain the proportionsygperc <- yeargenre/apply(yeargenre, 1, sum)ygperc <- ygperc[, c("Painting", "Sculpture", "Photography", "Print", "Other")]## Convert to DataFrame and reverse levels of factor for ggplotygpercm <-as.data.frame(ygperc)ygpercm$genre <-factor(ygpercm$genre, levels =rev(unique(ygpercm$genre)))## Create the ggplot objectg <-ggplot(ygpercm, aes(y = Freq, x = year, fill = genre)) +geom_bar(stat ="identity") +coord_flip() +labs(y =NULL, x =NULL, title ="Proportion of Genre of Art Sales") +theme(legend.position ="off") +geom_text(data = ygpercm[ygpercm$year ==2012& ygpercm$genre !="Other", ],aes(label = genre),position =position_stack(vjust =0.5),color ="white",size =4) +# Add the Other labelgeom_segment(aes(xend =16, yend =1, x =15, y =1.02),arrow =arrow(length =unit(0.15, "inches")),linewidth = .5, color ="black") +annotate("text", x =14.9, y =1.02, label ="Other", hjust =0, angle =270)## Pass the ggplot object into ggplotlyggplotly(g)
Part B: Genre’s Affect On Sales Price
Code
library(dplyr)
Attaching package: 'dplyr'
The following objects are masked from 'package:stats':
filter, lag
The following objects are masked from 'package:base':
intersect, setdiff, setequal, union
Code
## Median art sale pricesartmedian <-aggregate(art$price_usd, by =list(art$year, art$genre),FUN = median, na.rm =TRUE)names(artmedian) <-c("year", "genre", "price_usd")## 97.5% art sale pricesart975 <-aggregate(art$price_usd, by =list(art$year, art$genre),FUN = quantile, .975, na.rm =TRUE)names(art975) <-c("year", "genre", "price_usd")## Factorize Genre for ggplotartmedian$genre <-factor(artmedian$genre, levels =rev(unique(artmedian$genre)))art975$genre <-factor(art975$genre, levels =rev(unique(art975$genre)))artcombine <-bind_rows(artmedian %>%mutate(measure ="Median"), art975 %>%mutate(measure ="97.5%"))## Create gg objectg2 <-ggplot(artcombine, aes(x = year, y = price_usd, color = genre,linetype = measure)) +geom_line() +scale_y_continuous(name ="Price in Thousands USD",breaks =seq(0, 350000, by =50000),labels =paste(seq(0, 350, by =50), "k", sep ="") ) +scale_x_continuous(name =NULL,breaks =seq(1997, 2012, by =2),limits =c(1997, 2012) ) +labs(title ="Changes in Price by Genre") +scale_color_manual(values =1:5) +scale_linetype_manual(values =c("97.5%"="dotted", "Median"="solid")) +theme(legend.position ="inside",legend.position.inside =c(.1, .75),legend.background =element_blank(),legend.title =element_blank() )## Push gg object into plotlyggplotly(g2)
There is a change in sales price overtime w/ respect to outliers but not median. If mean was used then the outliers would have affected the mean price because they are so dramatic.
Problem 3 - data.table
Part A
Code
library(data.table)
Attaching package: 'data.table'
The following objects are masked from 'package:dplyr':
between, first, last
Code
library(nycflights13)## Read in 'flights' data w/ data.table functionflights <-data.table(flights)## Obtain departuresmergeDep <-merge(flights[, faa := origin], airports,by ="faa",all.x =TRUE)## Group by name and finalize tabledepart <- mergeDep[, .(meanDelay =mean(dep_delay, na.rm =TRUE),medianDelay =median(dep_delay, na.rm =TRUE)), by = name]depart
name meanDelay medianDelay
<char> <num> <num>
1: Newark Liberty Intl 15.10795 -1
2: John F Kennedy Intl 12.11216 -1
3: La Guardia 10.34688 -3